package com.example.gcode;

import android.app.Application;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.gcloud.dto.GCloudFile;

public class GCloudSingleton extends Application {

	private static GCloudSingleton gCloudSingleton = null;
	private List<Map<String, GCloudFile>> filesFromAllService;

	public GCloudSingleton() {
		filesFromAllService = new ArrayList<Map<String, GCloudFile>>();
	}

	public static GCloudSingleton getInstance() {
		if (GCloudSingleton.gCloudSingleton != null) {
			return GCloudSingleton.gCloudSingleton;
		}
		GCloudSingleton.gCloudSingleton = new GCloudSingleton();
		return GCloudSingleton.gCloudSingleton;
	}

	@Override
	public void onCreate() {		
		filesFromAllService = new ArrayList<Map<String, GCloudFile>>();
		if (GCloudSingleton.gCloudSingleton == null) {
			GCloudSingleton.gCloudSingleton = new GCloudSingleton();
		}
	}

	public List<Map<String, GCloudFile>> getFilesFromAllService() {
		try {
			if (filesFromAllService != null) {
				return filesFromAllService;
			}
			Thread thead = new Thread(new Runnable() {
				@Override
				public void run() {
					// TODO fill list from service
				}
			});
			thead.start();
		} catch (Exception e) {
			// TODO IO Exception
		}
		return filesFromAllService;
	}

	public void setFilesFromAllService(List<Map<String, GCloudFile>> filesFromAllService) {
		if (this.filesFromAllService != null
				&& !this.filesFromAllService.isEmpty()) {
			this.filesFromAllService.addAll(filesFromAllService);
		} else {
			this.filesFromAllService = filesFromAllService;
		}
	}

}
